package com.ndp.fb.fbagent.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ndp.fb.fbagent.service.AdCreativeFBService;
import com.ndp.fb.model.fbagent.param.adcreative.AdCreativeParam;
import com.restfb.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by infi on 2015/1/24.
 */
@Service
public class AdCreativeFBServiceImpl extends AbstractFBagentService implements AdCreativeFBService {
    private Logger logger = LoggerFactory.getLogger(AdCreativeFBServiceImpl.class);

    @Override
    public void createAdCreative(AdCreativeParam param) {
        logger.debug("createAdCreative>>>" + JSON.toJSONString(param));
        List<Parameter> params = adapter(param);
        logger.debug("createAdCreative List<Parameter>>>>" + params);
        String uri = "/act_" + param.getFbAccountId() + "/adcreatives";
        makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
    }

    @Override
    public void update(AdCreativeParam param) {
        if (!StringUtils.hasText(param.getCreativeId())) {
            logger.error("只能更新已有的creative.adCreativeId is null!");
        }else {
            String uri="/"+param.getCreativeId();
            List<Parameter> params = adapter(param);
            makePostRequest(uri, param.getAccessToken(), params.toArray(new Parameter[]{}));
        }
    }



    @Override
    public void delete(String adCreativeId,String token) {
        String uri = "/" + adCreativeId;
        makeDeleteRequest(uri,token);
    }
}
